package cn.iocoder.yudao.module.crossborder.platform.service.platformcategorymapping;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformcategorymapping.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformcategorymapping.PlatformCategoryMappingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformcategorymapping.PlatformCategoryMappingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商平台分类映射 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformCategoryMappingServiceImpl implements PlatformCategoryMappingService {

    @Resource
    private PlatformCategoryMappingMapper platformCategoryMappingMapper;

    @Override
    public Long createPlatformCategoryMapping(PlatformCategoryMappingSaveReqVO createReqVO) {
        // 插入
        PlatformCategoryMappingDO platformCategoryMapping = BeanUtils.toBean(createReqVO, PlatformCategoryMappingDO.class);
        platformCategoryMappingMapper.insert(platformCategoryMapping);

        // 返回
        return platformCategoryMapping.getId();
    }

    @Override
    public void updatePlatformCategoryMapping(PlatformCategoryMappingSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformCategoryMappingExists(updateReqVO.getId());
        // 更新
        PlatformCategoryMappingDO updateObj = BeanUtils.toBean(updateReqVO, PlatformCategoryMappingDO.class);
        platformCategoryMappingMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformCategoryMapping(Long id) {
        // 校验存在
        validatePlatformCategoryMappingExists(id);
        // 删除
        platformCategoryMappingMapper.deleteById(id);
    }

    @Override
        public void deletePlatformCategoryMappingListByIds(List<Long> ids) {
        // 删除
        platformCategoryMappingMapper.deleteByIds(ids);
        }


    private void validatePlatformCategoryMappingExists(Long id) {
        if (platformCategoryMappingMapper.selectById(id) == null) {
            throw exception(PLATFORM_CATEGORY_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public PlatformCategoryMappingDO getPlatformCategoryMapping(Long id) {
        return platformCategoryMappingMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformCategoryMappingDO> getPlatformCategoryMappingPage(PlatformCategoryMappingPageReqVO pageReqVO) {
        return platformCategoryMappingMapper.selectPage(pageReqVO);
    }

}